# -*- coding: utf-8 -*-
#
# File: PastebinIntegration.py
#
# Copyright (c) 2009 by ['Izhar Firdaus']
# Generator: ArchGenXML Version 2.3
#            http://plone.org/products/archgenxml
#
# GNU General Public License (GPL)
#

__author__ = """Izhar Firdaus <unknown>"""
__docformat__ = 'plaintext'

from AccessControl import ClassSecurityInfo
from Products.Archetypes.atapi import *
from zope.interface import implements
import interfaces

from Products.CMFDynamicViewFTI.browserdefault import BrowserDefaultMixin

from Products.ATContentTypes.content.link import ATLink
from Products.ATContentTypes.content.link import ATLinkSchema
from Products.OCFExternalIntegration.config import *

##code-section module-header #fill in your manual code here
from urllib2 import urlopen, URLError, HTTPError
import simplejson as json
##/code-section module-header

copied_fields = {}
copied_fields['remoteUrl'] = ATLinkSchema['remoteUrl'].copy()
copied_fields['remoteUrl'].widget.label = "Pastebin URL"
schema = Schema((

    copied_fields['remoteUrl'],


),
)

##code-section after-local-schema #fill in your manual code here
##/code-section after-local-schema

PastebinIntegration_schema = ATLinkSchema.copy() + \
    schema.copy()

##code-section after-schema #fill in your manual code here
##/code-section after-schema

class PastebinIntegration(ATLink):
    """
    """
    security = ClassSecurityInfo()

    implements(interfaces.IPastebinIntegration)

    meta_type = 'PastebinIntegration'
    _at_rename_after_creation = True

    schema = PastebinIntegration_schema

    ##code-section class-header #fill in your manual code here
    ##/code-section class-header

    # Methods
    def getRecentPosts(self):
        url = '%s/json.php' % self.remoteUrl
        try:
           u = urlopen(url)
        except URLError,e:
           return {}
        except HTTPError,e:
           return {}

        jsonfeed = u.read()
        contents = json.loads(jsonfeed)
        return contents['recentposts']

    def getRemoteUrl(self):
        return self.absolute_url()


registerType(PastebinIntegration, PROJECTNAME)
# end of class PastebinIntegration

##code-section module-footer #fill in your manual code here
##/code-section module-footer



